import { hasOwn, isPlainObject, uuid, _typeof } from 'sf-utils2';

interface IDataResponse {
  code?: number;
  message?: string;
  data?: any;
  responseId?: string;
  responseTime?: string;
}

interface IPage<T> {
  pageSize?: number;
  pageNum?: number;
  total?: number;
  pages?: number;
  dataList?: T;
}

/**
 * 返回体封装
 * @param code
 * @param message
 * @param data
 * @return {{code: number, data: {}, message: string, responseId: string, responseTime: number}}
 * @constructor
 */
export class ResultData {
  code: number;
  message: string;
  data: any;
  responseId: string;
  responseTime: string;

  constructor(code: number, message: string, data: any, responseId: string, responseTime: string);
  constructor(config: IDataResponse);
  constructor(arg1: number | IDataResponse, message?: string, data?: any, responseId?: string, responseTime?: string) {
    if (typeof arg1 === 'object') {
      // 处理对象形式
      const { code = 200, message = 'ok', data = null, responseId = uuid() } = arg1;
      this.code = code;
      this.message = message;
      this.data = data;
      this.responseId = responseId;
      this.responseTime = new Date().toISOString();
    } else {
      // 处理参数列表形式
      this.code = arg1;
      this.message = message!;
      this.data = data;
      this.responseId = responseId!;
      this.responseTime = responseTime!;
    }
  }

  /**
   * 请求成功
   * @param data
   * @return {{code: number, data: {}, message: string, responseId: string, responseTime: number}}
   */
  static ok<T>(data?: T) {
    return new ResultData({ code: 200, data: data });
  }

  /**
   * 请求失败500
   * @param message
   * @return {{code: number, data: {}, message: string, responseId: string, responseTime: number}}
   */
  static err(message: string) {
    return new ResultData({ code: 500, data: null, message });
  }

  /**
   * 请求失败
   * @param code
   * @param message
   * @return {{code: number, data: {}, message: string, responseId: string, responseTime: number}}
   */
  static error(code: number = 500, message: string = '服务端内部错误😅') {
    return new ResultData({ code, data: null, message });
  }

  /**
   * 请求分页
   * @param opts
   * @return {{code: number, data: {}, message: string, responseId: string, responseTime: number}}
   */
  static page(opts: { dataList: any[]; mark: any } = { dataList: [], mark: {} }) {
    return new ResultData({ data: { dataList: opts.dataList, mark: ResultData.mark(opts.mark) } });
  }

  /**
   * 分页
   * @param mark
   * @return {{current: number, totalSummarize: any, size: number, totalPageCount: number, totalRowCount: number, subtotalSummarize: any, currentRowCount: number}}
   */
  static mark<T>(mark: IPage<T>) {
    return mark;
  }
}

export default { ResultData };
